﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace dotsoftRAID_API
{
    public class API
    {
        private static XmlDocument LoadRAIDConfiguration()
        {
            XmlDocument configuration = new XmlDocument();
            configuration.Load(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "raid.xml"));
            return configuration;
        }

        public static void UnlockFile(string file)
        {            
            int unlock = Microsoft.VisualBasic.FileSystem.FreeFile();
            Microsoft.VisualBasic.FileSystem.FileOpen(unlock, file, Microsoft.VisualBasic.OpenMode.Random);
            Microsoft.VisualBasic.FileSystem.Unlock(unlock);
        }

        public static void InstallRAIDSystem(string type, string dev0, string[] RAID_DEVICES)
        {
            XmlElement raidNode = LoadRAIDConfiguration().CreateElement("raid");
            XmlAttribute typeAttribute = LoadRAIDConfiguration().CreateAttribute("type");
            typeAttribute.Value = type;
            XmlAttribute dev0Attribute = LoadRAIDConfiguration().CreateAttribute("dev0");
            dev0Attribute.Value = dev0;
            //TODO: Attribute is from another context
            raidNode.Attributes.Append(typeAttribute);
            raidNode.Attributes.Append(dev0Attribute);

            XmlAttribute dev1Attribute = LoadRAIDConfiguration().CreateAttribute("dev1");

            string devices = string.Empty;
            int i = 0;

            foreach (string device in RAID_DEVICES)
            {
                devices += string.Format("{0}{1}", device, i < (RAID_DEVICES.Length - 1) ? "-" : string.Empty);
                i++;
            }

            dev1Attribute.Value = devices;

            raidNode.Attributes.Append(dev1Attribute);

            LoadRAIDConfiguration().AppendChild(raidNode);
        }

        public static void DeinstallRAIDSystem(string type, string dev0, string[] RAID_DEVICES)
        {
        }
    }
}
